<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <style>
        html,
        body,
        #container {
            width: 100%;
            height: 100%;
        }
    </style>
    <title>折线的绘制和编辑</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
    <script src="https://webapi.amap.com/maps?v=1.4.15&key=f25894294feedfcbf8fcbddbc0349b1c"></script>
    <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
</head>
<body>
<div id="container"></div>
<div class="input-card" style="width: 120px">
    <button class="btn" onclick="polyEditor.open()" style="margin-bottom: 5px">开始编辑</button>
    <button class="btn" onclick="polyEditor.close()">结束编辑</button>
</div>
<script type="text/javascript">

    window.onload = function () {
        let url = "http://192.168.0.111:8080/json";
        let request = new XMLHttpRequest();
        request.open("get", url);
        request.send(null);
        request.onload = function () {
            if (request.status === 200) {
                let json = JSON.parse(request.responseText);
                let center = [];
                let path = [];

                for (let i = 0; i < json.length; i++) {
                    if (i === 0) {
                        center = [json[0]["lon"], json[0]["lat"]];
                    }
                    // AMap.convertFrom([json[i]["lon"], json[i]["lat"]], 'gps', function (status, result) {
                    //     console.log(status, result);
                    //
                    //     if (result.info === 'ok') {
                    //         let lnglats = result.locations; // Array.<LngLat>
                    //         console.log(lnglats);
                    //         path.push(lnglats)
                    //     }
                    // });
                    let lo = GPS.gcj_encrypt2(json[i]["lat"],json[i]["lon"]);
                    console.log(lo);
                    path.push(lo)
                    // path.push([json[i]["lon"], json[i]["lat"]])
                }
                // console.log(path);
                Draw(center,path)
            }
        };

        function Draw(center = [], path) {
            let map = new AMap.Map("container", {
                center: center,
                zoom: 14
            });
            let polyline = new AMap.Polyline({
                path: path,
                isOutline: true,
                outlineColor: '#ffeeff',
                borderWeight: 3,
                strokeColor: "#3366FF",
                strokeOpacity: 1,
                strokeWeight: 6,
                dirImg:'https://a.amap.com/jsapi_demos/static/images/mass0.png',
                // 折线样式还支持 'dashed'
                strokeStyle: "solid",
                // strokeStyle是dashed时有效
                strokeDasharray: [10, 5],
                lineJoin: 'round',
                lineCap: 'round',
                zIndex: 50,
            });

            polyline.setMap(map);
            // 缩放地图到合适的视野级别
            map.setFitView([polyline]);
            //
            // let polyEditor = new AMap.PolyEditor(map, polyline);
            //
            // polyEditor.on('addnode', function (event) {
            //     log.info('触发事件：addnode')
            // });
            //
            // polyEditor.on('adjust', function (event) {
            //     log.info('触发事件：adjust')
            // });
            //
            // polyEditor.on('removenode', function (event) {
            //     log.info('触发事件：removenode')
            // });
            //
            // polyEditor.on('end', function (event) {
            //     log.info('触发事件： end')
            //     // event.target 即为编辑后的折线对象
            // });
        }
    }

    var GPS = {
        PI : 3.14159265358979324,
        x_pi : 3.14159265358979324 * 3000.0 / 180.0,
        delta : function (lat, lon) {
            // Krasovsky 1940
            //
            // a = 6378245.0, 1/f = 298.3
            // b = a * (1 - f)
            // ee = (a^2 - b^2) / a^2;
            var a = 6378245.0; //  a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
            var ee = 0.00669342162296594323; //  ee: 椭球的偏心率。
            var dLat = this.transformLat(lon - 105.0, lat - 35.0);
            var dLon = this.transformLon(lon - 105.0, lat - 35.0);
            var radLat = lat / 180.0 * this.PI;
            var magic = Math.sin(radLat);
            magic = 1 - ee * magic * magic;
            var sqrtMagic = Math.sqrt(magic);
            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);
            dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);
            return {'lat': dLat, 'lon': dLon};
        },

        //GPS---高德
        gcj_encrypt2:function( wgsLat , wgsLon ){
            let lo = this.gcj_encrypt( wgsLat , wgsLon );
            // console.log(lo);
            return [lo.lon,lo.lat]
            // return [lo.lat,lo.lon]
        },
        gcj_encrypt : function ( wgsLat , wgsLon ) {
            if (this.outOfChina(wgsLat, wgsLon))
                return {'lat': wgsLat, 'lon': wgsLon};

            var d = this.delta(wgsLat, wgsLon);
            // console.log({'lat' : wgsLat + d.lat,'lon' : wgsLon + d.lon})
            return {'lat' : wgsLat + d.lat,'lon' : wgsLon + d.lon};
        },
        outOfChina : function (lat, lon) {
            if (lon < 72.004 || lon > 137.8347)
                return true;
            return lat < 0.8293 || lat > 55.8271;

        },
        transformLat : function (x, y) {
            var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
            ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
            ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
            ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
            return ret;
        },
        transformLon : function (x, y) {
            var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
            ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
            ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
            ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
            return ret;
        }
    };
</script>
</body>
</html>